#!/bin/bash

set -e

TAG=$(date +%y%m%d.%H%M)

function build-module() {
    local NAME="navia-$1"
    local VERSION="$(./mvnw -pl "$NAME" help:evaluate -Dexpression=project.version -q -DforceStdout)"
    local JAR="$NAME-$VERSION.jar"

    echo "=> Try to build module $NAME..."

    if test -d "$NAME"; then
      echo "=> Ready to build module $NAME."
    else
      echo "?? ERROR: Module $NAME NOT found!"
      exit 1
    fi

    echo "=> Try to package $NAME..."
    ./mvnw -pl "$NAME" clean package

    rm -rf dist
    mkdir dist

    mv "$NAME/target/$JAR" dist/app.jar

    echo "=> Try to build docker image of $NAME..."
    docker build -t "$NAME:$TAG" .

    rm -rf dist

    echo "=> Module $NAME has been built."
    echo
}


modules=(user gateway)
for mod in "${modules[@]}"
do
  build-module "$mod"
done